package zd.cornermemory.db;


import org.greenrobot.greendao.query.QueryBuilder;

import greendao.DaoMaster;
import greendao.DaoSession;
import zd.cornermemory.App;
import zd.cornermemory.utils.MyGreenDaoDbHelper;

/**
 * <pre>
 * GreenDao
 * </pre>
 * Created by Zhou on 17/7/12.
 */
public class GreenDaoManager {
    private DaoMaster  mDaoMaster;
    private DaoSession mDaoSession;
    private static volatile GreenDaoManager mInstance = null;
    private final static    String          DBNAME    = "BLINDFOLDED_DB";

    private GreenDaoManager() {
        if (mInstance == null) {
            MyGreenDaoDbHelper devOpenHelper = new
                    MyGreenDaoDbHelper(App.getInstance(), DBNAME);
            mDaoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
            mDaoSession = mDaoMaster.newSession();
            QueryBuilder.LOG_SQL = true;
            QueryBuilder.LOG_VALUES = true;
        }
    }

    public static GreenDaoManager getInstance() {
        if (mInstance == null) {
            synchronized (GreenDaoManager.class) {
                if (mInstance == null) {
                    mInstance = new GreenDaoManager();
                }
            }
        }
        return mInstance;
    }

    public DaoMaster getMaster() {
        return mDaoMaster;
    }

    public DaoSession getSession() {
        return mDaoSession;
    }

    public DaoSession getNewSession() {
        mDaoSession = mDaoMaster.newSession();
        return mDaoSession;
    }
}
